package com.medic.client.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.medic.entity.Assitant;
import com.medic.entity.Doctor;
import com.medic.entity.Pharmacist;
import com.medic.entity.Reply;
import com.medic.entity.Topic;
import com.medic.entity.User;
import com.medic.entity.Weichat;
import com.medic.service.impl.GroupServiceImpl;
import com.medic.service.impl.TopicServiceImpl;

@Controller
@RequestMapping("client/topic")
public class TopicController {
	
	@Resource
	private TopicServiceImpl topicService;
	
	

	@Resource
	private GroupServiceImpl groupService;
	

	
	@RequestMapping("/index")
	public String index(Model model,HttpSession session){

		String role = ((Weichat)session.getAttribute("weichat")).getRole();  
		
		String result =  "";
		try {
			switch(role){
			case "user": 
				
				model.addAttribute("groups",groupService.getGroupByUser((((User)session.getAttribute("user"))).getId()));
				result = "client/ask_user";
				break;
			case "doctor":  
				model.addAttribute("groups",groupService.getGroupByDoctor(((Doctor)session.getAttribute("doctor")).getId()));
				result = "client/ask_doctor";
				break;
			case "pharmacist": 
				model.addAttribute("groups", groupService.getGroupByPharmacist(((Pharmacist)session.getAttribute("pharmacist")) .getId()));
				result = "client/ask_doctor";
				break;
			case "assitant": 
				model.addAttribute("groups", groupService.getGroupByAssitant(((Assitant)session.getAttribute("assitant")).getId())); 		
				result = "client/ask_doctor";
				break;
			
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return result;
		
	}
	
	@RequestMapping("action/post/content")
	@ResponseBody
	public String postcontent(@Param("group")int group,HttpSession session ,@Param("content")String content){
		
		User user =(User)session.getAttribute("user");
		if(user == null) return "{}";
		String result="{}";
		try{
			Topic topic = new Topic();
			topic.setContent(content);
			topic.setCtime(new Timestamp(System.currentTimeMillis()).toString());
			topic.setSort(System.currentTimeMillis());
			
			if(content.length()>10) topic.setTitle(content.substring(0,10));
			else topic.setTitle(content.substring(0));
			
			
			topic.setUserid(user.getId() );
			topic.setAddtional(1);
			topic.setNoReadCount(0);
			topic.setGroupid(group);
			
			topicService.save(topic);
			
			
		}catch(Exception ee){
			ee.printStackTrace();
			result = "{\"status\":\"error\"}";
		}
		return result;
	}

	@RequestMapping("action/post")
	public String post(Model model,HttpSession  session){
		User user = (User)session.getAttribute("user");
		if(user==null) return "error";
		
		try{
			model.addAttribute("groups",groupService.getGroupByUser(user.getId()));
			
		}catch(Exception  ee){
			ee.printStackTrace();
		}
		
		return "client/ask_new";
	}
	
	@RequestMapping("action/detail/{topic}")
	public String reply(@PathVariable("topic")int topic,Model model ,HttpSession session){
		
		try{
			String isuser="";
			if((isuser = (String)session.getAttribute("isuser")) == null) return "error";
			Page<Reply> page=null;
			switch(isuser){ 
			case "true" :
				page = topicService.userGetReplyBy(1, topic);
				break;
			case "false" :
				page = topicService.otherGetReplyBy(1, topic);
				break;
			}
			model.addAttribute("topic",topicService.getTopicById(topic)) ;
			model.addAttribute("replys",page);
			
			int cur = page.getPageNum();
			model.addAttribute("pre", cur==1?-1:cur-1);
			model.addAttribute("next", cur==page.getPages()?-2:cur+1);
			model.addAttribute("topicid",topic);

		}catch(Exception e){
			e.printStackTrace();
		}
		return "client/ask_detail";
	}
	@ResponseBody
	@RequestMapping("action/detail/{topic}/{pageno}")
	public Object replypager(@PathVariable("pageno")int pageno,@PathVariable("topic")int topic,HttpSession session){
		
		Map<String,Object> object = new HashMap<String, Object>();
		
		try{
			String isuser="";
			if((isuser = (String)session.getAttribute("isuser")) == null) return "error"; 
			Page<Reply> page=null;
			switch(isuser){
				
			case "true" :
				page = topicService.userGetReplyBy(pageno, topic);
				break;
			case "false" :
				page = topicService.otherGetReplyBy(pageno, topic);
				break;
			}
			int cur = page.getPageNum();
			if(cur == 1) object.put("topic",topicService.getTopicById(topic) );
			object.put("data", page);
			object.put("pre", cur==1?-1:cur-1);
			object.put("next",cur!=page.getPages()?cur+1: -2);
		}catch(Exception e){
			e.printStackTrace();
		}
		return object;
	}
	
	

	@RequestMapping("action/detail/{topicid}/r")
	@ResponseBody() 
	public String r(@PathVariable("topicid")int topicid,@Param("content")String content,HttpSession  session){
		String isuser="";
		
		if((isuser = (String)session.getAttribute("isuser")) == null) return "error"; 
		try{
			
			Weichat weichat = (Weichat)session.getAttribute("weichat");
			Reply r = new Reply ();
			r.setReplyid(weichat.getRoleid());
			r.setTopicid(topicid);
			r.setContent(content);
			r.setRtime(new Timestamp(System.currentTimeMillis()).toString() );
			r.setSort(System.currentTimeMillis());
			switch(isuser){
			case "false" :
				topicService.otherReplyTo(r);
				break;
			case "true" : 
				topicService.userReplyTo(r);
				break;
			}
			
		}catch(Exception ee){
			ee.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		
		
		
		return "{}";
	}
	
	
	
	@ResponseBody
	@RequestMapping("action/upages/{group}/{pageno}")
	public Object uall(@PathVariable("group")int group,HttpSession session,@PathVariable("pageno")int pageno){
		Map<String,Object> object = new HashMap<String,Object>();
		
		User user=(User)session.getAttribute("user");
		if(user == null) return  "{}";
		
		try{
			
			//患者 角色拿咨询
			Page<Topic> page=topicService.getTopicByUser(pageno,user.getId() ,group);
			
			
			object.put("data",page);
			int cur = page.getPageNum();
			
			
			object.put("next", cur==page.getPages()?-2:cur+1);
			
			object.put("pre", cur==1?-1:cur-1);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return object;
	}
	
	@ResponseBody
	@RequestMapping("action/mpages/{group}/{pageno}")
	public Object mall(@PathVariable("group")int group,@PathVariable("pageno")int pageno){
		Map<String,Object> object = new HashMap<String,Object>();
		
		try{
			
			//医护角色拿咨询
			Page<Topic> page=topicService.getTopicByMedic(pageno,group);
			
			object.put("data",page);
			int cur = page.getPageNum();
			
			
			object.put("next", cur==page.getPages()?-2:cur+1);
			
			object.put("pre", cur==1?-1:cur-1);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return object;
	}
	

}
